import $V from './$V';

export default function vr(price, volume, length) {
    let vrValue = $V.init(price.length,0);
    let diff = $V.init(price.length,0);
    diff = $V.copy_by_index(diff,1,$V.sub($V.subv(price,1,price.length),$V.subv(price,0,price.length-1)));
    for(let i = 0;i < price.length;i++){
        if(i < length){
            let temp = $V.subv(volume,0,i+1);
            vrValue[i] = $V.sum($V.vector_of_index(temp,$V.where($V.subv(diff,0,i+1),">",0)))/($V.sum($V.vector_of_index(temp,$V.where($V.subv(diff,0,i+1),"<",0)))+ 0.5*$V.sum($V.vector_of_index(temp,$V.where($V.subv(diff,0,i+1),"==",0))))*100;
        }
        if(i >= length){
            let temp = $V.subv(volume,i-length+1,i+1);
            vrValue[i] = (
                $V.sum($V.vector_of_index(temp,$V.where($V.subv(diff,i-length+1,i+1),">",0)))
                + 0.5*$V.sum($V.vector_of_index(temp,$V.where($V.subv(diff,i-length+1,i+1),"==",0)))
            )/(
                $V.sum($V.vector_of_index(temp,$V.where($V.subv(diff,i-length+1,i+1),"<",0)))
                + 0.5*$V.sum($V.vector_of_index(temp,$V.where($V.subv(diff,i-length+1,i+1),"==",0))))*100;
        }
    }
    return vrValue;
}
